package com.mop.sg2.map;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MyView extends SurfaceView implements SurfaceHolder.Callback,
		Runnable {

	private SurfaceHolder holder;
	public static int x = 100, y = 100;

	public MyView(Context context) {
		super(context);
		Log.i("TAG", "MyView()");
		holder = this.getHolder();
		holder.addCallback(this);
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		Log.i("TAG", "surfaceChanged()");

	}

	public void surfaceCreated(SurfaceHolder holder) {
		new Thread(this).start();
		Log.i("TAG", "surfaceCreated()");
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.i("TAG", "surfaceDestroyed()");

	}

	public void run() {
		try {
			while (!Thread.currentThread().isInterrupted()) {
				synchronized (holder) {
					this.onDraw();
				}
				Thread.sleep(100);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	public void onDraw() {
		Canvas canvas = holder.lockCanvas();
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		Log.i("TAG", canvas.getWidth() + "|" + canvas.getHeight());
		canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
		paint.setColor(Color.RED);
		paint.setAntiAlias(true);
		canvas.drawCircle(x, y, 30, paint);
		// canvas.drawRect(x, y, a, b, paint);
		holder.unlockCanvasAndPost(canvas);
	}
}
